package Utility;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
/**
 * @category Utility module
 * @author Sean Lionheart
 * @version 1.0.0.0
 * The plain text log writer, which write the log into txt file.
 */
public class LogWriterText implements ILogWriter {
	
	private File logFile;			//The log file.
	/**
	 * Constructor, if the log file did not exist. Create a new file.
	 */
	public LogWriterText() {
		logFile = new File(LogContext.LOGTEXTFILENAME);
		try {
			logFile.createNewFile();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Write the information into the log file.
	 */
	public void writeLog(LogEntry entry) {
		synchronized (this) {
			try {
				BufferedWriter bWriter = 
					new BufferedWriter(new FileWriter(logFile,true));
				Date currentDate = new Date();
				bWriter.append(currentDate.toString());
				bWriter.append(" ");
				bWriter.append(entry.getCategoryString());
				bWriter.newLine();
				bWriter.append(entry.getLogMessage());
				bWriter.newLine();
				bWriter.flush();
				bWriter.close();
				
			}
			catch (IOException e) {
				// TODO: handle exception
			}
		}
	}
}
